package com.jrx.anytxn.common.uitl;

import com.jrx.anytxn.common.function.*;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.function.*;
import java.util.stream.Collector;

/**
 * @program: anytxn-cf-common
 * @description: collector功能工具类
 * @author: xiaoyu.jing
 * @date: 2019-10-13 14:27
 **/
public class CollectorsUtils {
    static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet();

    private CollectorsUtils() {
    }

    @SuppressWarnings("unchecked")
    private static <I, R> Function<I, R> castingIdentity() {
        return i -> (R) i;
    }

    /**
     * 对Collector进行扩展
     *
     * @param <T>
     * @param <A>
     * @param <R>
     */
    static class CollectorImpl<T, A, R> implements Collector<T, A, R> {
        private final Supplier<A> supplier;
        private final BiConsumer<A, T> accumulator;
        private final BinaryOperator<A> combiner;
        private final Function<A, R> finisher;
        private final Set<Characteristics> characteristics;

        CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
                      Function<A, R> finisher, Set<Characteristics> characteristics) {
            this.supplier = supplier;
            this.accumulator = accumulator;
            this.combiner = combiner;
            this.finisher = finisher;
            this.characteristics = characteristics;
        }

        CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
                      Set<Characteristics> characteristics) {
            this(supplier, accumulator, combiner, castingIdentity(), characteristics);
        }

        @Override
        public BiConsumer<A, T> accumulator() {
            return accumulator;
        }

        @Override
        public Supplier<A> supplier() {
            return supplier;
        }

        @Override
        public BinaryOperator<A> combiner() {
            return combiner;
        }

        @Override
        public Function<A, R> finisher() {
            return finisher;
        }

        @Override
        public Set<Characteristics> characteristics() {
            return characteristics;
        }
    }

    /**
     * 自定义收集器，对List对象中的BigDecimal属性累加求和
     *
     * @param mapper
     * @param <T>
     * @return
     */
    public static <T> Collector<T, ?, BigDecimal> summingBigDecimal(ToBigDecimalFunction<? super T> mapper) {
        return new CollectorImpl<>(() -> new BigDecimal[1], (a, t) -> {
            if (a[0] == null) {
                a[0] = BigDecimal.ZERO;
            }
            a[0] = a[0].add(mapper.apply(t));
        }, (a, b) -> {
            a[0] = a[0].add(b[0]);
            return a;
        }, a -> a[0], CH_NOID);
    }

    /**
     * 将受检异常转换成非受检异常
     *
     * @param mapper
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T, R> Function<T, R> tranToUncheck(UncheckedFunction<T, R> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        };
    }

    /**
     * 将受检异常转换成非受检异常
     *
     * @param mapper
     * @param <T>
     * @param <U>
     * @return
     */
    public static <T, U> BiConsumer<T, U> tranToUncheck(UncheckedBiConsumer<T, U> mapper) {
        Objects.requireNonNull(mapper);
        return (t, u) -> {
            try {
                mapper.accept(t, u);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        };
    }

    /**
     * 将受检异常转换成非受检异常
     *
     * @param mapper
     * @param <T>
     * @param <U>
     * @return
     */
    public static <T, U> Consumer<T> tranToUncheck(UncheckedConsumer<T> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                mapper.accept(t);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        };
    }
}